<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
    <script>
        // 如果异步返值又依赖另一个异步返回值, 那么回调的情况还会进一步变复杂。
        // 在实际的代码中, 这就要求嵌套回调
        // 随着代码越来越复杂, 回调策略是不具有扩展性的。这称为“回调地狱”。
        function double(value, success, failure) {
            setTimeout(() => {
                try {
                    if(typeof value !== 'number') {
                        throw "Must provide number as first argument";
                    }
                    success(2 * value);
                } catch (e) {
                    failure(e);
                }
            }, 1000);
        }

        const successCallback = (x) => {
            double(x, (y) => console.log(`Success: ${y}`));
        }
        const failureCallback = (e) => console.log(`Failure: ${e}`);

        double(3, successCallback, failureCallback);
        double('b', successCallback, failureCallback);

    </script>
</body>
</html>